/**
* Copyright 2009, Jethro Grassie, Creative Jar Technology Ltd (http://www.cjtech.co.uk)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
**/

package com.adobe.cairngorm.business;

import com.adobe.cairngorm.CairngormError;
import com.adobe.cairngorm.CairngormMessageCodes;

import java.util.HashMap;
import java.util.Iterator;

import javafx.io.http.HttpRequest;
import javafx.reflect.*;

public class RMIServices extends AbstractServices
{
    var services:HashMap = new HashMap();

    public override function register(serviceLocator:ServiceLocator):Void
    {         
        var iterator:Iterator = getAccessors(serviceLocator).iterator();
        var local:FXLocal = new FXLocal();
        var context:FXLocal.Context = local.getContext();
        var objVal:FXLocal.ObjectValue = new FXLocal.ObjectValue(serviceLocator, context);

        while(iterator.hasNext())
        {
            var variable:FXVarMember = iterator.next() as FXVarMember;
            var varname = variable.getName();
            var vartype:FXType = variable.getType();
            var varvalue = variable.getValue(objVal);
            if(context.findClass("com.adobe.cairngorm.business.RMIService")
                .isAssignableFrom(vartype))
            {
                if(not services.containsKey(varname))
                {
                    //println("Adding {varname}");
                    services.put(varname, varvalue);
                }
            }
        }
    }

    public override function getService(name:String):Object
    {
        if(not services.containsKey(name))
        {
            throw CairngormError
            {
                errorCode: CairngormMessageCodes.REMOTE_OBJECT_NOT_FOUND
                data: name
            };
        }
        var service:RMIService = (services.get(name) as FXLocal.Value).asObject() as RMIService;
        return service;
    }
}

